package com.qujia.station.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qujia.common.exception.BaseException;
import com.qujia.station.domain.PayOrder;
import com.qujia.station.mapper.PayOrderAllMapper;
import com.qujia.station.service.IPayOrderAllService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service("payOrderAllService")
@Slf4j
public class PayOrderAllServiceImpl implements IPayOrderAllService {

    @Autowired
    private PayOrderAllMapper payOrderAllMapper;

    @Override
    public void addNewPayOrder() {
        PayOrder payOrder = payOrderAllMapper.selectLast();
//        if(Objects.isNull(payOrder)){
//            payOrderAllMapper.insetPayOrderHistory();
//            payOrderAllMapper.insetPayOrder();
//            return;
//        }
        if(Objects.isNull(payOrder)){
            throw new BaseException("pay_order_all 数据表为空");
        }
        log.info("addNewPayOrder 同步订单前，最后一条订单：[{}]", JSONObject.toJSONString(payOrder));
        payOrderAllMapper.insetNewPayOrder(payOrder.getOrderId());
    }


    @Override
    public void addPayOrder(PayOrder payOrder) {
        if(Objects.isNull(payOrder)){
            throw new BaseException("请求参数异常");
        }

        payOrderAllMapper.insertSelective(payOrder);
    }

    @Override
    public void updatePayOrder(PayOrder payOrder) {
        if(Objects.isNull(payOrder)){
            throw new BaseException("请求参数异常");
        }
        payOrderAllMapper.updateByPrimaryKeySelective(payOrder);
    }

    @Override
    @Transactional
    public int deletePay(List<Long> list) {
        return payOrderAllMapper.deletePay(list);
    }

}
